/**
 * 修改登录密码 执行
 * @param newPsw 用户输入的新密码
 */
function ajaxUpdatePsw(newPsw) {
    //loading("数据提交中...");
    var url = "/Lvscore_User_M/account/ajaxUpdatePsw";
    $.ajax({
        url:url,
        type:"post",
        data:{'newPsw': newPsw},
        success:function (data) {
            //$(".loadingBox").hide();
            if (data == 1) {
                var cookie=new sweetCookie();
                cookie.setCookie("remember","",-1);
                window.location.href = "/Lvscore_User_M/userWeb/views/accounterCenter/changePsdSucc.html"
            }else if(data==-1){
                validateSpanMsg("newPsd1","新密码与旧密码不能相同");
            }else if(data==-2){
                validateSpanMsg("newPsd1","登录密码不能和支付密码相同");
            }
        },
        error:function(){
            //$(".loadingBox").hide();
            alertBox("网络异常，请重试");
        },
        dataType:"json"
    });
}
/**
 * 验证输入的原始登录密码是否正确
 * @param oldPsw    用户输入的原密码
 */
function ajaxValidateOldPsw(oldPsw) {
    var url = "/Lvscore_User_M/account/ajaxValidateOldPsw";
    $.ajax({
        url:url,
        type:"post",
        data:{'oldPsw': oldPsw},
        success:function (data) {
            var newPsd = $(".newPassword").val();
            if (data == 1) {
                ajaxUpdatePsw(newPsd);
            } else if(data==-1){
                validateSpanMsg("oldPsd","旧密码输入不正确");
                //alertBox("旧密码输入不正确");
            }else{
                alertBox("网络异常，请重试");
            }
        },
        error:function(){
            alertBox("网络异常，请重试");
        },
        dataType:"json"
    });
}

$(function () {
    $(".oldPassword").val("");
    $(".newPassword").val("");
    $(".sure").val("");
    //实时监控输入框
    inputChange(".oldPassword","#oldPsd");
    inputChange(".newPassword","#newPsd1");
    inputChange(".sure","#newPsd2");
    inputChange(".newPassword","#defrentError");
    inputChange(".sure","#defrentError");
    function oldPsdMessage(){
        var oldPsd = $(".oldPassword").val();
        if (oldPsd == null || oldPsd == "") {
            validateSpanMsg("oldPsd","请输入旧密码");
            return false;
        }else if(!(isPassword(oldPsd))){
            validateSpanMsg("oldPsd","旧密码错误");
            return false;
        }else{
            return true;
        }
    }
    function newPsdMessage1(){
        var newPsd = $(".newPassword").val();
        if (newPsd == null || newPsd == "") {
            validateSpanMsg("newPsd1","请输入新密码");
            return false;
        }else if(!(isPassword(newPsd))){
            validateSpanMsg("newPsd1","新密码格式不正确");
            return false;
        }else{
            return true;
        }
    }
    function newPsdMessage2(){
        var confPsd = $(".sure").val();
        var newPsd = $(".newPassword").val();
        if (confPsd == null || confPsd == "") {
            validateSpanMsg("newPsd2","请确认新密码");
            return false;
        }else if(newPsd != confPsd){
            validateSpanMsg("defrentError","两次输入的密码不一致");
            return false;
        }else{
            return true;
        }
    }
    $(".oldPassword").blur(function(){
        oldPsdMessage();
    })
    $(".newPassword").blur(function(){
        newPsdMessage1();
    })
    $(".sure").blur(function(){
        newPsdMessage2();
    })
    $(".sub").click(function () {
        var oldPsd = $(".oldPassword").val();
        var newPsd = $(".newPassword").val();
        var confPsd = $(".sure").val();
        if(oldPsdMessage()&&newPsdMessage1()&&newPsdMessage2()){
            ajaxValidateOldPsw(oldPsd);
        }
    })
});